package com.web.graduationpro.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.web.graduationpro.dao.service.ApplyService;
import com.web.graduationpro.dao.service.ArticleService;
import com.web.graduationpro.dao.service.GoodsService;
import com.web.graduationpro.dao.service.InstrumentCourseService;
import com.web.graduationpro.dao.service.InstrumentSchoolService;
import com.web.graduationpro.dao.service.MemberService;
import com.web.graduationpro.dao.service.OrderMiddleService;
import com.web.graduationpro.dao.service.OrderService;
import com.web.graduationpro.dao.service.ShoppingCartMiddleService;
import com.web.graduationpro.dao.service.ShoppingCartService;
import com.web.graduationpro.dao.service.TeacherService;
import com.web.graduationpro.model.Apply;
import com.web.graduationpro.model.InstrumentCourse;
import com.web.graduationpro.model.InstrumentSchool;
import com.web.graduationpro.model.Member;

@Controller
@RequestMapping("/apply/*")
public class ApplyController {

	@Resource(name="instrumentSchoolServiceImpl")
	private InstrumentSchoolService instrumentSchoolServiceImpl;
	
	@Resource(name="instrumentCourseServiceImpl")
	private InstrumentCourseService instrumentCourseServiceImpl;
	
	@Resource(name="teacherServiceImpl")
	private TeacherService teacherServiceImpl;
	
	@Resource(name="goodsServiceImpl")
	private GoodsService goodsServiceImpl;
	
	@Resource(name="articleServiceImpl")
	private ArticleService articleServiceImpl;
	
	@Resource(name="shoppingCartMiddleServiceImpl")
	private ShoppingCartMiddleService shoppingCartMiddleServiceImpl;
	
	@Resource(name="shoppingCartServiceImpl")
	private ShoppingCartService shoppingCartServiceImpl;
	
	@Resource(name="memberServiceImpl")
	private MemberService memberServiceImpl;
	
	@Resource(name="orderServiceImpl")
	private OrderService orderServiceImpl;
	
	@Resource(name="orderMiddleServiceImpl")
	private OrderMiddleService orderMiddleServiceImpl;
	
	@Resource(name="applyServiceImpl")
	private ApplyService applyServiceImpl;
	
	@RequestMapping(value = "/toapply", method = RequestMethod.GET)  
    public String toapply(HttpServletRequest request,HttpServletResponse reponse) {  
		try {
			request.setCharacterEncoding("UTF-8");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		reponse.setContentType("text/html;charset=UTF-8");
		
		Member member = null;
		int loginUser = 0;
		if(request.getSession().getAttribute("userid")!=null){
		   loginUser = (Integer) request.getSession().getAttribute("userid");
		}
		if(loginUser!=0){
			member = (Member) memberServiceImpl.findById(Member.class, loginUser);
		}
		
		if(member!=null){
			List<InstrumentSchool> ises = instrumentSchoolServiceImpl.findAll(InstrumentSchool.class);
			request.setAttribute("ises", ises);
			return "/apply";	
		}else{
			return "/login";
		}
    }  
	
	@RequestMapping(value = "/addapply", method = RequestMethod.POST)  
    public String addapply(HttpServletRequest request,HttpServletResponse reponse) {  
		try {
			request.setCharacterEncoding("UTF-8");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		reponse.setContentType("text/html;charset=UTF-8");
		
		Member member = null;
		int loginUser = 0;
		if(request.getSession().getAttribute("userid")!=null){
		   loginUser = (Integer) request.getSession().getAttribute("userid");
		}
		if(loginUser!=0){
			member = (Member) memberServiceImpl.findById(Member.class, loginUser);
		}
		
		if(member!=null){
			String returnStr = "/apply";
			String instrumentCourse = request.getParameter("instrumentCourse");
			String name = request.getParameter("name");
			String phone = request.getParameter("phone");
			String information = request.getParameter("information");
			
			if(instrumentCourse!=null && !"".equals(instrumentCourse.trim())){
				InstrumentCourse is = (InstrumentCourse) instrumentCourseServiceImpl.findById(InstrumentCourse.class, Integer.parseInt(instrumentCourse));
			    if(is!=null){
			    	Apply apply = new Apply();
			    	apply.setName(name);
			    	apply.setPhone(phone);
			    	apply.setInformation(information);
			    	apply.setInstrumentCourse(is);
			    	apply.setMember(member);
			    	
			    	applyServiceImpl.save(apply);
			    	returnStr = "/user/info";	
			    }
			}
			return returnStr;	
		}else{
			return "/login";
		}
    }  
	
	@RequestMapping(value = "/getCourseBySchool", method = RequestMethod.POST)  
    public void getCourseBySchool(HttpServletRequest request,HttpServletResponse reponse) {  
		try {
			request.setCharacterEncoding("UTF-8");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		reponse.setContentType("text/html;charset=UTF-8");
		String result = "";
		String id = request.getParameter("id");
		if(id!=null && !"".equals(id.trim())){
			Object[] cses = new Object[1];
			cses[0] = Integer.parseInt(id);
			String hql = "  FROM InstrumentCourse ic WHERE ic.instrumentSchool.id=?";
			List<InstrumentCourse> courses = instrumentCourseServiceImpl.findByPage(hql,cses,0,0);
			
			if(courses!=null && courses.size()!=0){
				List<InstrumentCourse> returnResultList = new ArrayList<InstrumentCourse>();
				for(InstrumentCourse ic:courses){
					ic.setSsSchool(ic.getInstrumentSchool().getSchoolName());
					returnResultList.add(ic);
				}
				JsonConfig jc = new JsonConfig();
				jc.setExcludes(new String[]{"instrumentSchool","applys"});
				JSONArray json = JSONArray.fromObject(returnResultList,jc);
				result = json.toString();
			}
		}
		
		try {
			PrintWriter writer = reponse.getWriter();
			writer.write(result);
			writer.flush();
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
    } 
}
